package cn.ac.jaas.isc.vegetable.quartz.util;

import cn.ac.jaas.isc.vegetable.common.core.util.SpringContextHolder;
import cn.ac.jaas.isc.vegetable.quartz.constants.JobTypeQuartzEnum;
import cn.ac.jaas.isc.vegetable.quartz.exception.TaskException;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 张高昌
 * @date 2022/4/30 16:27
 * @description: TaskInvoke工厂类
 */
@Slf4j
public class TaskInvokeFactory {

    /**
     * 根据对应jobType获取对应 invoker
     *
     * @param jobType
     * @return
     * @throws TaskException
     */
    public static ITaskInvoke getInvoker(String jobType) throws TaskException {
        if (StrUtil.isBlank(jobType)) {
            log.info("获取TaskInvoke传递参数有误，jobType:{}", jobType);
            throw new TaskException("");
        }

        ITaskInvoke iTaskInvoke = null;
        if (JobTypeQuartzEnum.JAVA.getType().equals(jobType)) {
            iTaskInvoke = SpringContextHolder.getBean("javaClassTaskInvoke");
        } else if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
            iTaskInvoke = SpringContextHolder.getBean("springBeanTaskInvoke");
        } else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
            iTaskInvoke = SpringContextHolder.getBean("restTaskInvoke");
        } else if (JobTypeQuartzEnum.JAR.getType().equals(jobType)) {
            iTaskInvoke = SpringContextHolder.getBean("jarTaskInvoke");
        } else if (StrUtil.isBlank(jobType)) {
            log.info("定时任务类型无对应反射方式，反射类型:{}", jobType);
            throw new TaskException("");
        }

        return iTaskInvoke;
    }
}
